<?php
namespace app\manager\model;

use think\Model;

class Camps extends \think\Model
{
    protected $autoWriteTimestamp = 'timestamp';
    protected $createTime = 'created_at';
    protected $updateTime = 'updated_at';
    
    protected $table = 'camps';
    // protected $privates = ['name','address'];

    protected $type = [
        'license' => 'array',
        'cover' => 'array'
    ];

    

    public function camptypes(){
        return $this->belongsTo('CampTypes','type');
    }

    public function campgrades(){
        return $this->belongsTo('CampGrades','grade');
    }

    public function branch(){
        return $this->belongsTo('Branchs','branch_id');
    }


    public function images()
    {
        return $this->hasMany('Images','camp_id');
    }
    public function getCamps()
    {
        return self::order('updated_at desc')->select();
    }
    /**
     * 获取营地分页列表信息
     * @param  string $order 排序
     * @param  array $query 附加筛选信息
     * @return [type]            [description]
     */
    public function getCampsLists($query = array(), $order = 'updated_at desc')
    {
        return $this->setDrafts(false)->setOrder($order)->getInfoList($query);
    }
    /**
     * 获取营地回收站列表信息
     * @param  string $order 排序
     * @param  array $query 附加筛选信息
     * @return [type]            [description]
     */
    public function getCampsListsOnlyTrashed($query = array(), $order = 'updated_at desc')
    {
        return $this->getListsOnlyTrashed($query, $order);
    }
    /**
     * 获取草稿箱列表信息
     * @param  string $order 排序
     * @param  array $query 附加筛选信息
     * @return [type]            [description]
     */
    public function getCampsListsDrafts($query = array(), $order = 'updated_at desc')
    {       
        return $this->getListsOnlyDrafts($query, $order);
    }
    
    /**
     * 创建新营地信息
     * @param  array $camp_info_array 营地信息数组
     * @return collection                  成功返回营地信息集合
     */
    public function createCamp($camp_info_array)
    {
        if (input('file.image')) {
            if($image = model('Upload','model\service')->go()){
                //$camp_info_array['image'] = $image->id;
            }
        }
       // return $this->addInfo($camp_info_array);
       $data=self::addInfo($camp_info_array);
       //dump($data);die;
       $data->updatePivot('campServices', $camp_info_array['services']);
       $data->updatePivot('campFacilities', $camp_info_array['facilities']);
       $data->updatePivot('campPlays', $camp_info_array['play']);
       return $data;
    }
    
    /**
     * 
     * 删除营地信息
     * @param  uuid $camp_id 要删除的营地主键
     * @return boll          成功后返回true
     */
    public function delCamp($camp_id,$forcedel=false)
    {
        return $this->delInfo($camp_id,$forcedel);
    }
    public function campRestore($id)
    {
        return $this->restoreInfo($id);
    }

    public function getType($id)
    {
        $types = model('CampTypes')->getCampTypes();
        $types = array_column($types->toArray(), 'name', 'id');
        if (array_key_exists($id, $types)) {
           return $types[$id];
        }
        return '无';
        //第二种方式
        // $types = model('CampTypes')->where('id',$id)->value('name');
        // return $types;
    }
    public function getGrade($id)
    {
        $grades = model('CampGrades')->getCampGrades();
        $grades = array_column($grades->toArray(), 'name', 'id');
        if (array_key_exists($id, $grades)) {
           return $grades[$id];
        }
        return '无';
    }

     public function setImg($id, $img ='')
    {
        $result = self::update(['img'=>$img], [$this->getPk()=>$id]);
        if ($result) {
            //成功后写入日志
            $this->tableLog('setInfo', ['img'=>$img]);
            return true;
        }
        return false;
    }


    // public function postIndex()
    // {
    //     if($f = model('Upload','model\service')->go()){
    //         return model('Upload','model\service')->getObjectUrl($f['url']);
    //     }
    // }

    public function getCampList()
    {
        $id = session('manager.id');
        $camp_id = model('CampManagers')->where('id',$id)->value('camp_id');
        return $this->where('id',$camp_id)->find();
    }

    public function createCamps($post_camp_info)
    {
        return $this->create($post_camp_info);
    }

    public function getCampInfo($id)
    {
        return $this->where('id',$id)->find();
    }

    public function updateCamp($id, $post_camp_info)
    {
        return $this->update($post_camp_info,['id'=> $id]);
    }

}